var editObj=null,ptable=null,treeGrid=null,tableId='treeTable',layer=null;
    layui.config({
        base: '/layuimanage/layui/extend/'
    }).extend({
        treeGrid:'treeGrid'
    }).use(['jquery','treeGrid','layer'], function(){
        var $=layui.jquery;
        treeGrid = layui.treeGrid;//很重要
        layer=layui.layer;
        ptable=treeGrid.render({
            id:tableId
            ,elem: '#'+tableId
			,method:"get"
            ,idField:'id'
            ,url:'/layuimanage/data2.json'
            ,cellMinWidth: 100
            ,treeId:'id'//树形id字段名称
            ,treeUpId:'pId'//树形父id字段名称
            ,treeShowName:'menuName'//以树形式显示的字段
            ,cols: [[
				{field:'menuName', width:300, title: '菜单名称'},
				{field:'sort',width:200,align:'center', title: '排序'},
				{field:'type', width:200,align:'center', title: '类型'},
				{field:'authorityKey', width:200,align:'center', title: '权限标识'},
				{field:'isHide', width:200,align:'center', title: '可见'},
				{field:'requestUrl',align:'center', title: '请求地址'},
                {width:200,title: '操作', align:'center'/*toolbar: '#barDemo'*/
                    ,templet: function(d){
                    var html='';
                    var addBtn='<a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>';
                    var delBtn='<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>';
                    return addBtn+delBtn;
					}
                }
            ]]
            ,page:false
        });

        treeGrid.on('tool('+tableId+')',function (obj) {
            if(obj.event === 'del'){//删除行
                del(obj);
            }else if(obj.event==="edit"){//编辑行
				console.log(obj.data);
                edit(obj.data);
            }
        });
    });

    function del(obj) {
        layer.confirm("你确定删除数据吗？如果存在下级节点则一并删除，此操作不能撤销！", {icon: 3, title:'提示'},
            function(index){//确定回调
                obj.del();
                layer.close(index);
            },function (index) {//取消回调
               layer.close(index);
            }
        );
    }
	
	function edit(Obj){
		layer.open({
			type:2,
			title: "菜单新增",
			area: ['1200px', '650px'],
			content:"/layuimanage/menumanage/add.html"
		})
	}

    var i=1000;
    //添加
    function add() {
		layer.open({
			type:2,
			title: "菜单新增",
			area: ['1200px', '650px'],
			content:"/layuimanage/menumanage/add.html"
		})
		
        // var param={};
        // param.name='水果'+Math.random();
        // param.id=++i;
        // param.pId=pObj?pObj.id:0;
        // treeGrid.addRow(tableId,pObj?pObj.LAY_TABLE_INDEX+1:0,param);
    }

	
    function print() {
        console.log(treeGrid.cache[tableId]);
        var loadIndex=layer.msg("对象已打印，按F12，在控制台查看！", {
            time:3000
            ,offset: 'auto'//顶部
            ,shade: 0
        });
    }